package Common;

/*************************************************************************************
* * ����: IniFile
* * �����ռ䣺IniFile
* * ����������һ��ģ��Delphi��TIniFile����,ʵ����Ini�ļ�����ز�����

* **********************************************************************************/



/** 
 һ��ģ��Delphi��TIniFile����
*/
public class IniFile
{
	public String FileName; //INI�ļ���

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#region ������дINI�ļ���API����

	private static native boolean WritePrivateProfileString(String section, String key, String val, String filePath);
	static
	{
		System.loadLibrary("kernel32");
	}
	private static native int GetPrivateProfileString(String section, String key, String def, byte[] retVal, int size, String filePath);

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#endregion


//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#region ���켰��������

	/** 
	 ��Ĺ��캯��������INI�ļ��� 
	 
	 @param AFileName Ҫ���ݵ�Ini�ļ���
	*/
	public IniFile(String AFileName)
	{
		// �ж��ļ��Ƿ����
		java.io.File fileInfo = new java.io.File(AFileName);
		//Todo:����ö�ٵ��÷�
		if ((!fileInfo.exists())) //|| (FileAttributes.Directory in fileInfo.Attributes))
		{
			throw (new RuntimeException("Ini�ļ�������"));
		}
		//��������ȫ·�������������·��
		FileName = fileInfo.getPath();
	}


	/** 
	 ȷ����Դ���ͷ�
	*/
	protected void finalize() throws Throwable
	{
		UpdateFile();
	}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#endregion


	/** 
	 ��ȡINI�ļ�ָ���Ľڵ��ַ���ֵ
	 
	 @param Section Ҫ��ȡ��ֵ�Ľ�
	 @param Ident Ҫ��ȡ�ļ���
	 @param Default �������ʧ�ܵ�Ĭ�Ϸ���ֵ
	 @return 
	*/
	public final String ReadString(String Section, String Ident, String Default)
	{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: Byte[] Buffer = new Byte[65535];
		byte[] Buffer = new byte[65535];
		int bufLen = GetPrivateProfileString(Section,Ident,Default,Buffer, Buffer.length - 1,FileName);
		//�����趨0��ϵͳĬ�ϵĴ���ҳ���ı��뷽ʽ�������޷�֧������
		String s = Encoding.GetEncoding(0).GetString(Buffer);
		// modified by den .
		s = s.substring(0,bufLen).replace("\0","");
		return s.trim();
	}

	/** 
	 ��ȡINI�ļ�ָ���Ľڵ�����ֵ
	 
	 @param Section Ҫ��ȡ��ֵ�Ľ�
	 @param Ident Ҫ��ȡ�ļ���
	 @param Default �������ʧ�ܵ�Ĭ�Ϸ���ֵ
	 @return 
	*/
	public final int ReadInteger(String Section, String Ident, int Default)
	{
		String intStr = ReadString(Section, Ident, String.valueOf(Default));
		try
		{
			return Integer.parseInt(intStr);
		}
		catch (RuntimeException ex)
		{
			System.out.println(ex.getMessage());
			return Default;
		}
	}

	/** 
	 ��ȡINI�ļ�ָ���ĽڵĲ���ֵ
	 
	 @param Section Ҫ��ȡ��ֵ�Ľ�
	 @param Ident Ҫ��ȡ�ļ���
	 @param Default �������ʧ�ܵ�Ĭ�Ϸ���ֵ
	 @return 
	*/
	public final boolean ReadBool(String Section, String Ident, boolean Default)
	{
		try
		{
			return Boolean.parseBoolean(ReadString(Section, Ident, String.valueOf(Default)));
		}
		catch (RuntimeException ex)
		{
			System.out.println(ex.getMessage());
			return Default;
		}
	}

	/** 
	 ��INI�ļ���ָ���Ľ�(Section)�е�Ident ��д���ַ���ֵ Value
	 
	 @param Section Ҫд��ֵ�Ľ�
	 @param Ident Ҫд��ļ���
	 @param Value Ҫд��ļ�ֵ
	*/
	public final void WriteString(String Section, String Ident, String Value)
	{
		if (!WritePrivateProfileString(Section, Ident,Value,FileName))
		{
			throw (new RuntimeException("дIni�ļ�����"));
		}
	}

	/** 
	 ��INI�ļ���ָ���Ľ�(Section)�е�Ident ��д������ֵ Value
	 
	 @param Section Ҫд��ֵ�Ľ�
	 @param Ident Ҫд��ļ���
	 @param Value Ҫд��ļ�ֵ
	*/
	public final void WriteInteger(String Section, String Ident, int Value)
	{
		WriteString(Section, Ident, String.valueOf(Value));
	}

	/** 
	 ��INI�ļ���ָ���Ľ�(Section)�е�Ident ��д��дBoolֵ Value
	 
	 @param Section Ҫд��ֵ�Ľ�
	 @param Ident Ҫд��ļ���
	 @param Value Ҫд��ļ�ֵ
	*/
	public final void WriteBool(String Section, String Ident, boolean Value)
	{
		WriteString(Section, Ident, String.valueOf(Value));
	}

	/** 
	 ��Ini�ļ��У���ָ����Section�����е�����Ident��ӵ��б���
	 
	 @param Section Ҫ��ȡֵ���б�Ľ�
	 @param Idents Ҫ�洢�б�Ľṹ
	*/
	public final void ReadSection(String Section, StringCollection Idents)
	{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: Byte[] Buffer = new Byte[16384];
		byte[] Buffer = new byte[16384];
		//Idents.Clear();

		int bufLen = GetPrivateProfileString(Section, null, null, Buffer, Buffer.length - 1,FileName);
		//��Section���н���
		GetStringsFromBuffer(Buffer, bufLen, Idents);
	}

	/** 
	 ��Buffer���ж�ȡֵ���б�
	 
	 @param Buffer Buffer ����
	 @param bufLen Buffer ����
	 @param Strings �б�
	*/
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)
	private void GetStringsFromBuffer(byte[] Buffer, int bufLen, StringCollection Strings)
	{
		Strings.Clear();
		if (bufLen != 0)
		{
			int start = 0;
			for (int i = 0; i < bufLen; i++)
			{
				if ((Buffer[i] == 0) && ((i - start) > 0))
				{
					String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
					Strings.Add(s);
					start = i + 1;
				}
			}
		}
	}


	/** 
	 ��Ini�ļ��У���ȡ���е�Sections������
	 
	 @param SectionList Ҫ�����Ľڵ�����
	*/
	public final void ReadSections(StringCollection SectionList)
	{
		//Note:�������Bytes��ʵ�֣�StringBuilderֻ��ȡ����һ��Section
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] Buffer = new byte[65535];
		byte[] Buffer = new byte[65535];
		int bufLen = 0;
		bufLen = GetPrivateProfileString(null, null, null,Buffer,Buffer.length - 1, FileName);
		GetStringsFromBuffer(Buffer, bufLen, SectionList);
	}

	/** 
	 ��ȡָ����Section������Value���б���
	 
	 @param Section Ҫ�����Ľڵ�����
	 @param Values ֵ���б�
	*/
	public final void ReadSectionValues(String Section, tangible.RefObject<NameValueCollection> Values)
	{
		StringCollection KeyList = new StringCollection();
		ReadSection(Section, KeyList);
		Values.argValue.Clear();
		for (String key : KeyList)
		{
			Values.argValue.Add(key, ReadString(Section, key, ""));
		}
	}

	/** 
	 ���ĳ��Section
	 
	 @param Section Section ����
	*/
	public final void EraseSection(String Section)
	{
		//
		if (!WritePrivateProfileString(Section, null, null, FileName))
		{
			throw (new RuntimeException("�޷����Ini�ļ��е�Section"));
		}
	}

	/** 
	 ɾ��ĳ��Section�µļ�
	 
	 @param Section Section ����
	 @param Ident ����
	*/
	public final void DeleteKey(String Section, String Ident)
	{
		WritePrivateProfileString(Section, Ident, null, FileName);
	}

	/** 
	 ����д��Ini �ļ���Buffer����
	 ע��:����Win9X����˵��Ҫʵ��UpdateFile�����������е�����д���ļ�
	 ��Win NT, 2000��XP�ϣ�����ֱ��д�ļ���û�л��壬���ԣ�����ʵ��UpdateFile
	 ִ�����Ini�ļ����޸�֮��Ӧ�õ��ñ��������»�������
	*/
	public final void UpdateFile()
	{
		WritePrivateProfileString(null, null, null, FileName);
	}

	/** 
	 ���ĳ��Section�µ�ĳ����ֵ�Ƿ����
	 
	 @param Section ָ����Section��
	 @param Ident ָ���ļ���
	 @return �ɹ�����true,���򷵻�false
	*/
	public final boolean ValueExists(String Section, String Ident)
	{
		//
		StringCollection Idents = new StringCollection();
		ReadSection(Section, Idents);
		return Idents.indexOf(Ident) > -1;
	}



}